using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class CBS_ListAttachmentService : BaseAppService<CBS_ListAttachmentDto, CBS_ListAttachment, CBS_ListAttachmentRepository>
    {
        readonly ExcelHelp _excelHelp;
        public CBS_ListAttachmentService(CBS_ListAttachmentRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<CBS_ListAttachment, CBS_ListAttachmentDto>
            GetLoadPageOffsetMode(PagingModel<CBS_ListAttachmentDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new CBS_ListAttachment()
            {
                Id = model.Id,
                CBS_ListId = model.CBS_ListId,//
                Name = model.Name,//
                Path = model.Path,
                Attachment = model.Attachment,//
   
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<CBS_ListAttachmentDto>> GetList(PagingModel<CBS_ListAttachmentDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
            //var pageModelEntity = await this.ExportDataAsync(loadPageOffsetModel);
            //IPageModel<CBS_ListAttachmentDto> pageModelDto = new PageModel<CBS_ListAttachmentDto>()
            //{
            //    PageData = pageModelEntity,
            //    RowCount = pageModelEntity.Count,
            //};
            //return pageModelDto;
        }


        public async Task<string>
            Export(PagingModel<CBS_ListAttachmentDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.CBS_ListId,
                CBS_ListId_title = "",
                model.Name,
                Name_title = "",
                model.Attachment,
                Attachment_title = "",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
